package cn.pping.utils.test;

import cn.pping.utils.ESClientUtil;
import com.sun.org.apache.xpath.internal.operations.Gte;
import com.sun.org.apache.xpath.internal.operations.Lte;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EsTest2 {
    // 添加数据
    @Test
    public void addTest() {
        // 调用工具类获取用户端对象
        TransportClient client = ESClientUtil.getClient();
        // 添加对象
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "pet", "1");
        // 创建一个Map
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("id", 3L);
        objectObjectHashMap.put("name", "你好");
        objectObjectHashMap.put("sex", 0);
        // 添加
        IndexResponse indexResponse = indexRequestBuilder.setSource(objectObjectHashMap).get();
        // 关闭
        client.close();
    }

    // 查询
    @Test
    public void findTest() {
        // 获取工具类
        TransportClient client = ESClientUtil.getClient();
        // 获取ES索引
        Map<String, Object> sourceAsMap = client.prepareGet("pethome", "pet", "1").get().getSourceAsMap();
        System.out.println(sourceAsMap);
        client.close();

    }

    // 修改数据
    @Test
    public void uptTest() {
        TransportClient client = ESClientUtil.getClient();
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "pet", "1");
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("id", 3L);
        objectObjectHashMap.put("name", "你好世界");
        objectObjectHashMap.put("sex", 1);
        UpdateResponse updateResponse = updateRequestBuilder.setDoc(objectObjectHashMap).get();
        client.close();

    }

    // 删除
    @Test
    public void deleleTest() {
        TransportClient client = ESClientUtil.getClient();
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "pet", "1");
        DeleteResponse deleteResponse = deleteRequestBuilder.get();
        client.close();
    }

    /**
     * @Description: 批量添加数据
     * @Author: Director
     * @Date: 2022/7/23 0:02
     * @return: void
     **/
    @Test
    public void testBatch() throws Exception {
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 3.批量创建文档，放入批量操作对象中
        for (int i = 2; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "user", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "我在源码学习Java!" + i)
                            .field("age", i - 1)
                            .field("sex", i % 2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 4.关闭连接
        client.close();
    }

    /**
     * 综合案例
     * - 查询用户表
     * - name包含：我在源码
     * - age在1~12之间
     * - sex=1
     * - 每页大小2
     * - 从2页开始查
     * - 按照age倒序
     */
    @Test
    public void searchTest() {
        TransportClient client = ESClientUtil.getClient();
        // 开启查找
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("pethome");
        // 创建一个Boot
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

                        boolQueryBuilder.must( QueryBuilders.matchQuery("name", "源码"));

                        boolQueryBuilder.filter(QueryBuilders.termQuery("sex",1))
                                        .filter(QueryBuilders.rangeQuery("age"). gte(1).lte(12));

        searchRequestBuilder.setQuery(boolQueryBuilder);

        searchRequestBuilder.setSize(2);
        searchRequestBuilder.setFrom(3);
        searchRequestBuilder.addSort("age",SortOrder.DESC);
        SearchHit[] hits = searchRequestBuilder.get().getHits().getHits();
        for (SearchHit hit : hits){
            System.out.println(hit);
        }
        client.close();

    }


}
